﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Mani.Common.WorkflowEngine;

namespace RastinArgham.CRM.Business
{
    public partial class Order
    {
        private string statusName;

        public string ProductName
        {
            get
            {
                if (this.ProductId.HasValue)
                    return this.Product.ProductName;
                else
                    return String.Empty;
            }
        }

        public long TotalPrice
        {
            get
            {
                // Discount Rules
                int quantity = this.Quantity.Value;

                if (this.ServiceId.HasValue)
                {
                    if (quantity == 12)
                        quantity = 9;
                    else if (quantity == 6)
                        quantity = 5;
                }

                long totalPrice = Convert.ToInt64((this.Price.Value * quantity));                
                
                // adding tax
                totalPrice += Convert.ToInt64(totalPrice * (this.TaxPercent.Value / 100));
                return totalPrice;
            }
        }

        public String ServicePeriod {
            get
            {
                if (!this.ServiceId.HasValue)
                    return String.Empty;
                else
                    return this.Service.ServicePeriod + " ماهه ";
            }
        }

        public string ServiceName
        {
            get
            {
                if (this.ServiceId.HasValue)
                    return this.Service.ServiceName;
                else
                    return String.Empty;
            }
        }

        public string Status
        {
            get
            {
                if (!String.IsNullOrEmpty(statusName))
                    return statusName;

                if (!this.WorkflowInstanceId.HasValue || !this.WorkflowStateId.HasValue)
                    return String.Empty;

                if (this.ServiceId.HasValue)
                {
                    statusName = (new ServiceOrderWorkflow()).GetInternalStateName(this.WorkflowStateId.Value);
                }
                else
                {
                    statusName = (new ProductOrderWorkflow()).GetInternalStateName(this.WorkflowStateId.Value);
                    if ( this.WorkflowStateId.Value == 120 )
                        statusName += " - " + this.CancelationReason.Description;
                }

                return statusName;
            }
        }

        public string CityName
        {
            get
            {
                if (this.Contact != null)
                {
                    return this.Contact.CityName;
                }
                else
                {
                    return String.Empty;
                }
            }
        }

        public string OperatorName
        {
            get
            {
                return CRMRepository.getUserNameByOperatorId(this.OperatorId);
            }
        }

        public String AccountManagerName
        {
            get
            {
                return CRMRepository.getUserNameByOperatorId(this.Contact.AccountManagerId);
            }
        }

        public string Serial
        {
            get
            {
                if (this.StockItems.Count == 0 && this.ServiceId.HasValue)
                {
                    // it's a service order
                    Order modemOrder = this.Contact.GetModemOrder();
                    if (modemOrder != null)
                        return modemOrder.Serial;
                }
                else if ( this.StockItems.Count != 0 && this.ProductId.HasValue )
                {
                    return this.StockItems.FirstOrDefault().SerialNumber;
                }

                return String.Empty;
            }
        }

        public string PaymentCode
        {
            get
            {
                if (this.StockItems.Count == 0 && this.ServiceId.HasValue)
                {
                    // it's a service order
                    Order modemOrder = this.Contact.GetModemOrder();
                    if (modemOrder != null)
                        return modemOrder.PaymentCode;
                }
                else if (this.StockItems.Count != 0 && this.ProductId.HasValue)
                    return this.StockItems.FirstOrDefault().PaymentCode;
                
                return String.Empty;
            }
        }

        public string SubID
        {
            get
            {
                if (this.StockItems.Count == 0 && this.ServiceId.HasValue)
                {
                    // it's a service order
                    return this.Contact.GetUsername();
                }
                else if (this.StockItems.Count != 0 && this.ProductId.HasValue)
                {
                    string username = this.StockItems.FirstOrDefault().SubID.Trim();

                    if (username.Length > 3 && username.IndexOf("20000") != 0)
                        username = "20000" + username;

                    return username;
                }

                return String.Empty;
            }
        }

        public string ContactTitle
        {
            get
            {
                return this.Contact.ContactTitle;
            }
        }

        public string ContactEditLink
        {
            get
            {
                return this.Contact.EditLink;
            }
        }

        public string LastComment
        {
            get
            {
                if ( !this.ServiceId.HasValue || ( this.WorkflowStateId != 88 && this.WorkflowStateId != 102 && this.WorkflowStateId != 106 ) )
                    return String.Empty;

                if (this.Workflow.LastComment == "طرح اشتراکی را انتخاب نمایید")
                    return "طرح اشتراک " + this.Service.MobinServiceName + " در مبین یافت نشد";

                if (this.Workflow.LastComment == "داده ای یافت نشد")
                    return "داده ای یافت نشد. احتمالا شناسه کاربری مودم اشتباه است";

                return this.Workflow.LastComment;
            }
        }
    }
}
